<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="关闭" ><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title" id="common-modal-label"><span id="classes-name"></span>班 签到历史</h4>
</div>
<div class="modal-body no-padding-top">
	<div id="titlebar">
	 	<span class="btn-group">
	  	<button type="button" class="btn btn-default btn-sm" onclick="handlerSwitch(-1)">
	    	<i class="glyphicon glyphicon-chevron-left"></i>
	  	</button>
	  	<button type="button" class="btn btn-default btn-sm" onclick="handlerSwitch(0)" id="classes-time"></button>
	  	<button type="button" class="btn btn-default btn-sm" onclick="handlerSwitch(1)" >
	    	<i class="glyphicon glyphicon-chevron-right"></i>
	    </button>
		</span>
		<span class="btn-group">
	    <span class="btn btn-info btn-sm" id="classes-remark"></span>
		</span>
	</div>
  <table data-toolbar="#titlebar" 
			 	 data-check-on-init="true" 
			 	 data-click-to-select="true" 
			 	 data-mobile-responsive="true" id="table-history">
	</table>
</div>
<div class="modal-footer">
  <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
</div>

<script type="text/javascript">
<!--
	//打开签到历史窗口
	function onHistoryShow(event){
		var _opts = jQuery(event.relatedTarget).data();
		jQuery(".modal-dialog","#common-modal").addClass("modal-fill");
		
		jQuery("#classes-name").text(classes.name);
		jQuery("#classes-remark").text(_opts.extention);
		
		if(!jQuery("#table-history").hasClass("table")){
			jQuery("#table-history").bootstrapTable().on("post-body.bs.table",function(event){
				jQuery("[data-toggle='tooltip']").tooltip({html:true, container:"html", trigger:'hover'});
				jQuery("body").popover({
					trigger: 'focus',
					selector: '.has-popover'
				});
			});
		}
		
		createHistory($moment());
	}

	//关闭签到历史窗口
	function onHistoryHidden(event){
		jQuery("#table-history").bootstrapTable("removeAll");
		jQuery("#table-student").bootstrapTable("refresh");
		jQuery(".modal-dialog","#common-modal").removeClass("modal-fill");
	}
	
	// 渲染签到历史的单元格
	var urlmap = {"上课":"/miwawa/{dbname}/student/check/undo","缺席":"/miwawa/{dbname}/student/check/undo", "请假":"/miwawa/{dbname}/student/leave/undo"};
	function renderWeek(value,row,index){
		var array = value ? value.split(",") : [""],
				checkid = array[0],
				status = array[1],
				actor = array[2],
				times = array[3],
				reason = array[4],
				dates = this.field;
		switch(status){
			case "上课":
			case "请假":
				row["checkId"] = checkid;
				return "<button class='"+(status == "上课" ? "btn btn-success" : "btn btn-warning")+" btn-xs has-popover' data-placement='top' data-content='"+createPopover(array)+"'>"
							+	" <i class='"+(actor && actor.split(":")[0] == "家长" ? "icon-user-female" : "icon-user")+"'></i> " + status
							+"</button> "
							+"<a class='btn btn-default btn-xs' onclick='handlerUndo(this, \""+dates+"\")' data-url='"+urlmap[status]+"' data-params='"+JSON.stringify(row)+"'>取消</a>";
			case "缺席":
				row["checkId"] = checkid;
				return "<button class='btn btn-danger btn-xs has-popover' data-placement='top' data-content='"+createPopover(array)+"'>"
							+	" <i class='"+(actor && actor.split(":")[0] == "家长" ? "icon-user-female" : "icon-user")+"'></i> " + "缺勤" 
							+"</button> "
							+"<a class='btn btn-default btn-xs' onclick='handlerUndo(this, \""+dates+"\")' data-url='"+urlmap[status]+"' data-params='"+JSON.stringify(row)+"'>取消</a>";
			default: return "";
		}
	}
	
	// 创建签到历史的表头
	function createTitle(date, week){
		return "<span class='btn btn-default btn-xs'>"+date+" "+week+"</span> "
	}
	
	// 创建签到历史的操作按钮
	function createButton(date){
		if($moment().isAfter(date)){
			var buttons = '<span class="btn-group">'
				 					+		'<button type="button" class="btn btn-success btn-xs" onclick="handlerCheck(this, \''+date+'\')" data-toggle="tooltip" data-placement="top" title="签到"><i class="glyphicon glyphicon-ok"></i></button>'
				 					+		'<button type="button" class="btn btn-danger  btn-xs" onclick="handlerLeave(this, \''+date+'\')" data-toggle="tooltip" data-placement="top" title="请假"><i class="glyphicon glyphicon-ban-circle"></i></button>'
				 					+ '</span>';
			return buttons;
		}else{
			var buttons = '<span class="btn-group">'
				 					+		'<button type="button" class="btn btn-danger  btn-xs" onclick="handlerLeave(this, \''+date+'\')" data-toggle="tooltip" data-placement="top" title="请假"><i class="glyphicon glyphicon-ban-circle"></i></button>'
					 				+ '</span>';
			return buttons;
		}
	}
	
	// 创建上课情况按钮的提示框
	function createPopover(array){
		if(array[1]=="上课"){
			return array[2]+"，于["+array[3]+"]签到";
		}else if(array[1]=="请假"){
			return array[2]+"，于["+array[3]+"]"+array[1]+"，请假理由："+array[4];
		}else{
			return array[2]+"，于["+array[3]+"]处理";
		}
	}
	
	// 取消签到或请假
	function handlerUndo(trigger, date){
		jQuery.message.confirm("确定要"+trigger.innerText+"？", function(confirm){
			var _opts = jQuery(trigger).data();
			if(confirm){
				jQuery.ajaxsend({
					url: _opts.url.replace("{dbname}",dbname),
					data: _opts.params,
					success: function(data){
						if(data && data.success){
							createHistory($moment(date));
						}
					}
				});
			}
		});
	}

	// 签到
	function handlerCheck(trigger, date){
		var rows = jQuery("#table-history").bootstrapTable("getSelections");
				date = date ? $moment(date) : $moment();
				
		if(rows.length <= 0){
			jQuery.message.warning("请选择要签到的学员！");
		}else{
			jQuery.utils.loading("正在生成签到数据，请稍后...", "show", 2000);
			
			var params = {
				"entityClass":"com.app.miwawa.api.eam.entity.StudentCheck",
				"beans":[]
			};
			jQuery.each(rows,function(i,row){
				if(row.item == "早教" && row.rested < classes.usecount){
					jQuery.message.alert("学员"+row.student+"的剩余课时不足，无法签到。请手动终止合同，并修改续约合同的总课时数。");
					return;
				}else{
					if(row.stopState != "stop" && row.status != "上课" && row.status != "请假"){
						params.beans.push({
							"checkDate": date.format("YYYY-MM-DD"),
							"checkTime": $moment().format("HH:mm"),
							"student":row.student,
							"studentId":row.studentId,
							"classes":classes.name,
							"classesId":classes.id,
							"actor":"老师:"+username,
							"actorId":userid,
							'contractId': row.contractId,
							'item':row.item,
							"status":"0",
							"invalid":"F"
						});
					}
				}
			});
			jQuery.ajaxsend({
				url:"/miwawa/{dbname}/student/check/all".replace("{dbname}",dbname),
				data:params,
				success:function(data){
					jQuery("[data-toggle='tooltip']","#table-history").tooltip("destroy");
					jQuery.utils.loading(null, "hide");
					createHistory(date);
				}
			});
		}
	}
	
	// 请假
	var leavedate, selectrows;
	function handlerLeave(trigger, date){
		var key = date;
		selectrows = jQuery("#table-history").bootstrapTable("getSelections");
		leavedate = date ? $moment(date) : $moment();
				
		if(selectrows.length <= 0){
			jQuery.message.warning("请选择要请假的学员！");
		}else if(selectrows.length > 1){
			jQuery.message.warning("每次只能为一名学员请假！");
		}else if(selectrows[0][key] != "" && selectrows[0][key] != undefined){
			jQuery.message.warning("所选学员已经签到，不能请假！");
		}else{
			if(selectrows[0].toplimit != ""){
				if(selectrows[0].toplimit >= 0 && selectrows[0].toprested == 0 && (!$moment().isAfter(date))){
					jQuery.message.alert("请假次数已用完，无法请假！可在班级管理修改该学员的请假次数上限！");
				}else{
					jQuery("#extension-modal").data("url","content/eam/classes/dialog/leave.html")
																		.data("onshown","onLeaveShown")
																		.data("onhidden","onLeaveHidden")
																		.modal("show");
				}
			}else{
				jQuery("#extension-modal").data("url","content/eam/classes/dialog/leave.html")
				.data("onshown","onLeaveShown")
				.data("onhidden","onLeaveHidden")
				.modal("show");
			}
		}
	}
	
	// 打开请假窗口时回调
	function onLeaveShown(event){
		require(['jquery.plugin.form'],function(form){
			form.chosen("#reason").select("#reason");
			
			form.chosen("#parentId").select("#parentId",{
				querys:{"customerId_EQ":selectrows[0].customerId}
			},function(chosen){
				jQuery("#student",".form-editor-leave").val(selectrows[0].student);
				jQuery("#studentId",".form-editor-leave").val(selectrows[0].studentId);
				jQuery("#classes",".form-editor-leave").val(classes.name);
				jQuery("#classesId",".form-editor-leave").val(classes.id);
				jQuery("#contractId",".form-editor-leave").val(selectrows[0].contractId);
				jQuery("#leaveDate",".form-editor-leave").val(leavedate.format("YYYY-MM-DD"));
				jQuery("#actor",".form-editor-leave").val("老师:"+username);
				jQuery("#actorId",".form-editor-leave").val(userid);
				
				chosen.change(function(event, value){
					jQuery("#parent").val(jQuery("#parentId > option[value='"+value.selected+"']").text());
				});
			});
		});
	}
	
	// 关闭请假窗口时回调
	function onLeaveHidden(event){
		createHistory(leavedate);
		jQuery("[data-toggle='tooltip']","#table-history").tooltip("destroy");
		jQuery(".form-editor-leave").resetForm();
	}
	
	//缺勤页面
	/* else if(selectrows[0].toplimit >= 0 && selectrows[0].toprested == 0){
		jQuery("#extension-modal").data("url","content/eam/classes/dialog/absent.html")
															.data("onshown","onLeaveShown")
															.data("onhidden","onLeaveHidden")
															.modal("show");
	} */
	
//-->
</script>